package com.example.springbootdatamock.mock;

import com.apifan.common.random.RandomSource;
import com.example.springbootdatamock.mapper.CKMapper;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Component
@RestController
public class CkMocker
        implements Runnable {
    @Value("${sleepMs.ckMockSleep}")
    private Integer mockSleep;
    @Autowired
    private CKMapper mapper;
    private static final Logger log = LoggerFactory.getLogger(CkMocker.class);

    @RequestMapping("/ckMock")
    public void run() {
        String[] brands = {"ZTE", "小米", "VIVO", "OPPO", "华为", "IPHONE", "荣耀", "REALME", "IQQO", "ONEPLUS"};

        Double[] amount = {Double.valueOf(2999.0D), Double.valueOf(3999.0D), Double.valueOf(4399.0D), Double.valueOf(5999.0D)};

        this.mapper.createTable();
        log.info("ckMock 建表成功");
        DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dateTime = null;
        int flag = 0;


        while (true) {
            flag = RandomUtils.nextInt(0, 3);

            switch (flag) {
                case 0:
                    dateTime = RandomSource.dateTimeSource().randomPastTime(7);
                    break;
                case 1:
                    dateTime = RandomSource.dateTimeSource().randomPastTime(LocalDateTime.now().minusDays(1L), 7200L);
                    break;
                case 2:
                    dateTime = RandomSource.dateTimeSource().randomPastTime(LocalDateTime.now(), 100L);
                    break;
            }

            this.mapper.insertData(dateTime.format(df1), brands[
                            RandomUtils.nextInt(0, brands.length)],
                    BigDecimal.valueOf(amount[RandomUtils.nextInt(0, amount.length)].doubleValue()), dateTime
                            .format(df2));

            log.info("ckMock 插入数据成功");

            try {
                Thread.sleep(this.mockSleep.intValue());

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
    }
}



 